#include "AObject.h"
#include "AUtils.h"
#include <math.h>

float CAUtils::Rand()
{
	int n = rand();
	float r = ((float)(n % 0x7fff)) / ((float)0x7fff);
	_Assert(r >= 0);
	_Assert(r < 1.0f);
	return r;
}

float CAUtils::Rand(float mi, float ma)
{
	if (ma <= mi) return mi;
	float di = ma - mi;
	float r = mi + CAUtils::Rand() * di;
	return r;
}

float CAUtils::getRotation(float y, float x)
{
	float angle = R2A(atan2(y, x));
	return angle;
}

bool CAUtils::almostEqual(float a, float b, float threhold)
{
    float delta = a - b;
    if (delta < 0) delta = -delta;
	return delta < threhold;
}

CCSize CAUtils::getDistance(const CCRect& r1, const CCRect& r2)
{
	CCSize ret = CCSizeZero;
	if (CCRect::CCRectIntersectsRect(r1, r2))
	{
		return ret;
	}

	ret.width	= r1.origin.x - (r2.origin.x + r2.size.width);
	ret.height	= r1.origin.y - (r2.origin.y + r2.size.height);

	return ret;
}
